
#include <gameEngine.h>
#include "CGameSceneFactory.h"
#ifdef _IRR_PSP_
#include "common.h"

#include <pspdebug.h>

#define printf  pspDebugScreenPrintf

#endif

#include <exception>
using namespace std;
#include "os.h"

class myexception: public exception
{
	virtual const char* what() const throw()
	{
		return "My exception happened";
	}
} myex;

using namespace game;

using namespace scene;

#ifndef _IRR_PSP_
#pragma comment(lib, "Irrlicht.lib")
#endif
irr::IrrlichtDevice* device;
void Func(void*a)
{
		device = irr::createDevice();	
         for(int i=0;i<1000;i++)
         {
             printf("2");
             irr::io::IWriteFile* file = device->getFileSystem()->createAndWriteFile("test.log", true);
             file->write("1",1);
             file->drop();
         }


                 irr::io::IReadFile* file = device->getFileSystem()->createAndOpenFile("test.log");
            
                for(int i=0;i<1000;i++)
         {
             char str[2];
           
             file->read(str,1);
             printf(str);
         }     
     


//        delete []str;
         
	/*	irr::scene::ISceneManager* smgr = device->getSceneManager();
	
	    irr::scene::IAnimatedMesh* mesh = smgr->getMesh("ms0:/media/TestBox.b3d");

	
	    irr::scene::IAnimatedMeshSceneNode* sNode = smgr->addAnimatedMeshSceneNode(mesh);

        sNode->setScale(irr::core::vector3df(50,50,50));

	    sNode->setMaterialFlag(irr::video::EMF_LIGHTING, false);   */

	/*while (device->run())
	{
		device->getVideoDriver()->beginScene(true, true, irr::video::SColor(255,100,101,140));
		device->getSceneManager()->drawAll();
	    device->getVideoDriver()->endScene();	
	}  */

							 
}

int main()
{
#ifdef _IRR_PSP_
	setupPSP();
	pspDebugScreenInit();
#endif

    
    // Func((void*)"");

	// irr::os::Printer::log("jkfdhjkfhsdjkhfjkdshfjkshkj");


	irrThread::delegateThreadCallback* callback = new irrThread::delegateThreadCallback;
	*callback += NewDelegate(&Func);

     try
	{
        irrThread* VideoThread = new irrThread(callback, "TEST", (void*)"");
	    VideoThread->start();
        	VideoThread->join();
            	delete VideoThread;
	}
	catch (exception& e)
	{
        printf("exception main %s",e.what());
        device->getLogger()->log("exception");
       device->getLogger()->log( e.what());
		//printf("%s\n", e.what());
	}
	         


      //     delete []str;
	//	while(true){};


	//irr::io::IWriteFile* file = device->getFileSystem()->createAndWriteFile("ms0:/bbb.txt", true);
	//file->write("bbb", 4);
	 while(true)
	 {
	 }


	//while(true){}
	//irr::IrrlichtDevice* device = irr::createDevice();	
	//
	//device->getSceneManager()->addAnimatedMeshSceneNode(device->getSceneManager()->getMesh("./resource/data/ninja.b3d"))->setMaterialFlag(irr::video::EMF_LIGHTING, false);
	//device->getSceneManager()->addCameraSceneNodeFPS();
	/*while (device->run())
	{
	device->getVideoDriver()->beginScene(true, true, irr::video::SColor(255,100,101,140));

	device->getSceneManager()->drawAll();

	device->getVideoDriver()->endScene();

	}
	*/

	/*game::IDeviceManager* devManager = game::getDeviceManager();


	while(!devManager->getIsVideoDeviceRun())
	{
	game::irrThread::sleep(100);
	}

	registerSceneTypes(devManager->getGameSceneManager());

	game::scene::IGameScene* loadScene = devManager->getGameSceneManager()->getGameScene("./config/ingamescene.xml");

	devManager->getGameSceneManager()->setCurrentGameScene(loadScene);

	irr::scene::ICameraSceneNode* camera = devManager->getSceneManager()->addCameraSceneNodeFPS();
	camera->drop();

	devManager->getVideoThread()->join();

	while(true)
	{
	}*/
	//engine::CBaseScene scene;
	//IrrlichtDevice *device = createDevice();

	//IVideoDriver* driver = device->getVideoDriver();
	//ISceneManager* smgr = device->getSceneManager();
	//IGUIEnvironment* guienv = device->getGUIEnvironment();


	//gui::IGUIStaticText* statusText = guienv->addStaticText(L"Hello World!", rect<int>(10,10,260,22), true);

	//IAnimatedMesh* mesh =  smgr->getMesh("./media/chars/SteamTrost01_Run.b3d");
	//IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(mesh);

	//IAnimatedMesh* mesh1 =  smgr->getMesh("./media/chars/SteamShoulderr01_Run.b3d");
	//IAnimatedMeshSceneNode* node1 = smgr->addAnimatedMeshSceneNode(mesh1);

	//IAnimatedMesh* mesh2 =  smgr->getMesh("./media/chars/SteamHat01_Run.b3d");
	//IAnimatedMeshSceneNode* node2 = smgr->addAnimatedMeshSceneNode(mesh2);

	//IAnimatedMesh* mesh3 =  smgr->getMesh("./media/chars/SteamBodyUp01_Run.b3d");
	//IAnimatedMeshSceneNode* node3 = smgr->addAnimatedMeshSceneNode(mesh3);

	//IAnimatedMesh* mesh4 =  smgr->getMesh("./media/chars/SteamBody01_Run.b3d");
	//IAnimatedMeshSceneNode* node4 = smgr->addAnimatedMeshSceneNode(mesh4);


	//node->setMaterialFlag(EMF_LIGHTING, false);


	//IAnimatedMesh* mesh1 =  smgr->getMesh("./media/testbox.b3d");
	//smgr->addAnimatedMeshSceneNode(mesh1)->setMaterialFlag(EMF_LIGHTING, false);

	//ICameraSceneNode* cam =  smgr->addCameraSceneNodeFPS(node);
	//cam->setPosition(cam->getPosition() + irr::core::vector3df(3600,100,3500));



	//devManager->drop();

#ifdef _IRR_PSP_
	sceKernelSleepThreadCB();
#endif
	return 0;
}




